package www.ph.adali.happy;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.viewpager.widget.ViewPager;

import www.ph.adali.happy.adapter.GuideAdapter;
import www.ph.adali.happy.animator.DepthPageTransformerAnimator;
import www.ph.adali.happy.view.CircleNavigatorView;

import java.util.ArrayList;
import java.util.List;

/*
 * 引导页
 */

public class GuideActivity extends BaseActivity implements CircleNavigatorView.OnCircleClickListener, ViewPager.OnPageChangeListener, View.OnClickListener {

    //    引导页全部的页面
    private List<View> mAllGuideViews;
    //    引导页的滑动组件
    private ViewPager mGuideViewPager;
    //    引导页适配器
    private GuideAdapter mGuideAdapter;
    //    viewpager滑动动画
    private DepthPageTransformerAnimator mDepthPageTransformerAnimator;
    //    圆点指示器FrameLayout
    private FrameLayout mCircleIndicatorFrameLayout;
    //    圆点指示器
    private CircleNavigatorView mCircleNavigator;
    private Button mStartLoginBtn;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_guide;
    }

    @Override
    public void onBindView() {
        mGuideViewPager = (ViewPager) this.findViewById(R.id.lemon_loan_view_pager);
        mCircleIndicatorFrameLayout = (FrameLayout) this.findViewById(R.id.circle_guide_frame_layout);
        mStartLoginBtn = (Button) this.findViewById(R.id.start_login_btn);
        mStartLoginBtn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mAllGuideViews = new ArrayList<View>();
        mGuideAdapter = new GuideAdapter(mAllGuideViews);
        mDepthPageTransformerAnimator = new DepthPageTransformerAnimator();
        LayoutInflater layoutInflater = getLayoutInflater().from(this);
        View guideOne = layoutInflater.inflate(R.layout.lemon_loan_guide_one, null);
        View guideTwo = layoutInflater.inflate(R.layout.lemon_loan_guide_two, null);
        View guideThree = layoutInflater.inflate(R.layout.lemon_loan_guide_three, null);
        mAllGuideViews.clear();
        mAllGuideViews.add(guideOne);
        mAllGuideViews.add(guideTwo);
        mAllGuideViews.add(guideThree);
        mGuideViewPager.setAdapter(mGuideAdapter);
        mGuideViewPager.setPageTransformer(true, mDepthPageTransformerAnimator);
        mGuideViewPager.addOnPageChangeListener(this);
        mCircleNavigator = new CircleNavigatorView(this);
        mCircleNavigator.setCircleCount(mAllGuideViews.size());
        mCircleNavigator.setCircleClickListener(this);
        mCircleIndicatorFrameLayout.removeAllViews();
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mCircleIndicatorFrameLayout.addView(mCircleNavigator, lp);
    }

    private void changeStartBtnVisible(int index) {
        if (index == mAllGuideViews.size() - 1) {
            mStartLoginBtn.setVisibility(View.VISIBLE);
        } else {
            mStartLoginBtn.setVisibility(View.INVISIBLE);
        }
    }

    //    点击圆点指示器的点击事件
    @Override
    public void onClick(int index) {
        if (mGuideViewPager != null) {
            mGuideViewPager.setCurrentItem(index);
            changeStartBtnVisible(index);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mCircleNavigator != null) {
            mCircleNavigator.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }
        changeStartBtnVisible(position);
    }

    @Override
    public void onPageSelected(int position) {
        if (mCircleNavigator != null) {
            mCircleNavigator.onPageSelected(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (mCircleNavigator != null) {
            mCircleNavigator.onPageScrollStateChanged(state);
        }
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(GuideActivity.this, MainActivity.class));
        this.finish();
    }
}